package com.example.java_doc_searcher.searcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

public class Parser {
    private static final String INPUT_Path= "D:/doc_searcher_index/jdk-8u421-docs-all/docs/api";
    Index index = new Index();
    private AtomicLong t1 = new AtomicLong(0);
    private AtomicLong t2 = new AtomicLong(0);
     public void run()  {
         //
         System.out.println("索引制作开始");
         long beg = System.currentTimeMillis();
         ArrayList<File> fileList = new ArrayList<>();
         enumFile(INPUT_Path,fileList);
         long endEnumFile = System.currentTimeMillis();
         System.out.println("枚举文件完毕! 消耗时间: " + (endEnumFile - beg) + "ms");
      //   System.out.println(fileList);
     //    System.out.println(fileList.size());
         for(File f:fileList){
             System.out.println("开始解析"+f.getAbsolutePath());
             parseHTML(f);
         }
         index.save();
         System.out.println("索引制作完毕");
     }
public void runByThread() throws InterruptedException  {
         long beg =   System.currentTimeMillis(); ;
                 System.out.println("索引制作开始！");
    ArrayList<File> files = new ArrayList<>();
    enumFile(INPUT_Path,files);
    CountDownLatch latch = new CountDownLatch(files.size());
    ExecutorService executorService = Executors.newFixedThreadPool(8);
    for(File f:files){
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("解析"+f.getAbsolutePath());
                parseHTML(f);
                latch.countDown();
            }
        });
    }
    latch.await();
    index.save();
    executorService.shutdown();
    long end =   System.currentTimeMillis();
    System.out.println("索引制作完毕！消耗时间:"+ (end-beg) + "ms");
    System.out.println("t1: " + t1 + ", t2: " + t2);
}
    private void parseHTML(File f)  {
         String title  = parseTitle(f);
        String  url =  parseUrl(f) ;
        long beg = System.nanoTime()  ;
         String content = parseContentByRegex(f);
        long mid = System.nanoTime()  ;
        index.addDoc(title, url, content);
        long end = System.nanoTime()  ;
        t1.addAndGet(mid - beg);
        t2.addAndGet(end - mid);
    }

    private String parseUrl(File f) {
        String part1 = "https://docs.oracle.com/javase/8/docs/api/";
        String part2 = f.getAbsolutePath().substring(INPUT_Path.length());
         return part1+part2;
    }

    public String parseContent(File f)  {
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(f), 1024 * 1024)) {

            boolean iscopy = true;
            StringBuilder content = new StringBuilder();
            while (true) {
                int ret = bufferedReader.read();

                if (ret == -1) {
                    break;
                }
                char c = (char)ret;
                if(iscopy){
                    if(c == '<'){
                        iscopy = false;
                        continue;
                    }
                    if(c == '\n' || c == '\r'){
                        c = ' ' ;
                    }
               content.append(c);
                }else{

                    if(c == '>'){{
                        iscopy = true;
                    }
                    }
                }

            }
            return content.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    private String parseTitle(File f) {
       String name =  f.getName();
        return name.substring(0,name.length()-".html".length());
    }

    private String readFile(File f) {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(f))) {
            StringBuilder content = new StringBuilder();
            while (true) {
                int ret = bufferedReader.read();
                if (ret == -1) {
                    break;
                }
                char c = (char)ret;
                if (c == '\n' || c == '\r') {
                    c = ' ';
                }
                content.append(c);
            }
            return content.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    public String parseContentByRegex(File f) {
        // 1. 先把整个文件都读到 String 里面
        String content = readFile(f);
        // 2. 替换掉 script 标签
        content = content.replaceAll("<script.*?>(.*?)</script>", " ");
        // 3. 替换掉普通的 html 标签
        content = content.replaceAll("<.*?>", " ");
        // 4. 使用正则把多个空格, 合并成一个空格
        content = content.replaceAll("\\s+", " ");
        return content;
    }
    private void enumFile(String inputPath, ArrayList<File> fileList) {

            File rootPath = new File(inputPath);
            File[] files = rootPath.listFiles();
            for(File f:files){
                if(f.isDirectory()){
                    enumFile(f.getAbsolutePath(),fileList);
                }else{
                    if(f.getAbsolutePath().endsWith(".html")){
                        fileList.add(f);
                    }
                }

            }

    }


    public static void main(String[] args) throws InterruptedException {
         Parser parser = new Parser();
         parser.runByThread();

    }
}
